﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text.log;
using MediatR;
using Microsoft.Extensions.Logging;
using NHibernate.UserTypes;
using Traceability.Register.Application.Commonds.TraceabilityManagementCommonds;
using Traceability.Register.Domain;
using Traceability.Register.ErrorCode.Result;
using Traceability.Register.Infrastructrue;

namespace Traceability.Register.Application.CommondHandlers.TraceabilityManagementCommondHandlers
{
    /// <summary>
    /// 省信息处理程序
    /// </summary>
    public class ProvinCommondHanlder : IRequestHandler<ProvinCommond, ApiResult>
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ProvinCommondHanlder> logger;
        /// <summary>
        /// 地点仓储
        /// </summary>
        private readonly IBaseRepository<Addresses> repository;
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="repository">地点仓储</param>
        public ProvinCommondHanlder(ILogger<ProvinCommondHanlder> logger, IBaseRepository<Addresses> repository)
        {
            this.logger = logger;
            this.repository = repository;
        }
        /// <summary>
        /// 处理程序
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult> Handle(ProvinCommond request, CancellationToken cancellationToken)
        {
            try
            {
                var result =await repository.QueryAsync(x => x.Level == 0);
                return ApiResult.Ok(result.ToList());
            }
            catch (Exception ex)
            {
                logger.LogError("省信息处理程序异常：" + ex.Message);
                throw;
            }
        }
    }
}
